/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.mdc.hearts.ejb.gameLogic;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author Administrator
 */
@Stateless
@EJB(name="java:global/HeartsBOTSessionBean", beanInterface=HeartsBOTSessionBeanLocal.class)
public class HeartsBOTSessionBean implements HeartsBOTSessionBeanLocal {
    @EJB
    private HeartsSessionBeanLocal heartsSessionBean;

    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    @Override
    public void playCard(final HeartsModel model, final int playerIndex) 
    {
        Card playingCard = null;
        ArrayList<Card> playableCard = heartsSessionBean.getPlayableCards(model, playerIndex);
        
        if(playableCard.isEmpty())
        {
            Logger.getLogger(HeartsBOTSessionBean.class.getName()).log(Level.SEVERE, " Error: 0 playable card!!!!");
            return;
        }
        
        if(playableCard.size() == 1)
        {
            playingCard = playableCard.get(0);
        }
        else if(model.nGameTurnSeed >= 0)
        {
            Card minGreaterCard = null;
            Card maxSmallerCard = null;
            int nMaxValue = -1;
            for(int i = 0; i < 4; i++)
            {
                if(model.playerCardPlayed[i] != null)
                {
                    if(model.playerCardPlayed[i].getSeed() == model.nGameTurnSeed)
                    {
                        nMaxValue = Math.max(model.playerCardPlayed[i].getValue(), nMaxValue);
                    }
                }
            }
            
            for(int i = 0; i < playableCard.size(); i++)
            {
                Card card = playableCard.get(i);
                if(card.getSeed() == model.nGameTurnSeed)
                {
                    if(card.getValue() > nMaxValue)
                    {
                        if(maxSmallerCard != null)
                        {
                            if(card.getValue() < maxSmallerCard.getValue())
                            {
                                maxSmallerCard = card;
                            }
                        }
                        else
                        {
                            maxSmallerCard = card;
                        }
                    }
                    else
                    {
                        if(minGreaterCard != null)
                        {
                            if(card.getValue() > minGreaterCard.getValue())
                            {
                                minGreaterCard = card;
                            }
                        }
                        else
                        {
                            minGreaterCard = card;
                        }
                    }
                }
            }
            
            playingCard = maxSmallerCard;
            if(playingCard == null)
            {
                playingCard = minGreaterCard;
            }
        }

        if(playingCard == null)
        {
            playingCard = playableCard.get(0);
            for(int i = 0; i < playableCard.size(); i++)
            {
                if(playingCard.getValue() > playableCard.get(i).getValue())
                {
                    playingCard = playableCard.get(i);
                }
                else if(playingCard.getValue() == playableCard.get(i).getValue())
                {
                    if(playingCard.getSeed() > playableCard.get(i).getSeed())
                    {
                        playingCard = playableCard.get(i);
                    }
                }
            }
        }
        
        
        if(!heartsSessionBean.playCard(model, playerIndex, playingCard))
        {
            System.out.println("cazzo carta non giocabile");
        }
    }

}
